package com.lmbx.shzh.schedule;

import com.google.gson.Gson;
import com.lmbx.shzh.domain.vo.AlarmList;
import com.lmbx.shzh.domain.vo.AlarmListVO;
import com.lmbx.shzh.mapper.RainMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class RainScheduleTask {

    @Autowired
    private RainMapper mapper;
    @Value("${alarmListUrl}")
    private String alarmListUrl;
    @Autowired
    private RestTemplate restTemplate;

    @Scheduled(fixedDelay = 10 * 60 * 1000L)
    public void scheduleAlarmList() {
        System.out.println("scheduleAlarmList ==================>");
        selectAlarmListToDB();
        System.out.println("scheduleAlarmList <==================");
    }

    private void selectAlarmListToDB() {
        String dataUrl = alarmListUrl + "&pageNo=1&pageSize=1";
        Gson gson = new Gson();
        String countUrl = alarmListUrl + "&pageNo=1&pageSize=1";
        ResponseEntity<String> resultEntity = restTemplate.getForEntity(countUrl, String.class);
        String body = resultEntity.getBody();
        AlarmList alarmList = gson.fromJson(body, AlarmList.class);
        int total = alarmList.data.total;
        if (total <= 0) {
            return;
        }
        dataUrl = alarmListUrl + "&pageNo=1&pageSize="+total;
        ResponseEntity<String> totalEntity = restTemplate.getForEntity(dataUrl, String.class);
        String totalBody = totalEntity.getBody();
        AlarmList resultAlarms = gson.fromJson(totalBody, AlarmList.class);
        List<AlarmList.DataBean.RowsBean> rows = resultAlarms.data.rows;

        for (AlarmList.DataBean.RowsBean row : rows) {
            AlarmListVO vo = new AlarmListVO();
            vo.alarmId = row.alarmId;
            vo.alarmTime = row.alarmTime;
            vo.stationName = row.stationName;
            List<String> region = row.regionInfo;
            if (region != null && region.size() >= 5) {
                vo.xian = region.get(3);
            }
            vo.endTime = row.endTime;
            vo.regionIndexCode = row.regionIndexCode;
            vo.lgtd = String.format("%.6f", row.longitudeDegree + (row.longitudeMinute * 60 + row.longitudeSecond) / 3600.0f);
            vo.lttd = String.format("%.6f", row.latitudeDegree + (row.latitudeMinute * 60 + row.latitudeSecond) / 3600.0f);
            vo.cameraid = mapper.getCameraIdByRegionId(vo.regionIndexCode);
            vo.stcd = mapper.getStcdByCameraId(vo.cameraid);

            Map<String, String> map = mapper.getAlarmById(vo.alarmId);
            if (map == null || map.size() <= 0) {
                mapper.insertAlarm(vo);
            } else {
                mapper.updateAlarm(vo);
            }
        }
    }

}
